package hierarchy

import (
	"gitcode.com/G-YT/iHealth25/server/global"
	"gitcode.com/G-YT/iHealth25/server/model/common/request"
	"gitcode.com/G-YT/iHealth25/server/model/common/response"
	"gitcode.com/G-YT/iHealth25/server/model/hierarchy"
	hierarchyReq "gitcode.com/G-YT/iHealth25/server/model/hierarchy/request"
	"gitcode.com/G-YT/iHealth25/server/service"
	"gitcode.com/G-YT/iHealth25/server/utils"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type LevelTwoApi struct {
}

var levelTwoService = service.ServiceGroupApp.HierarchyServiceGroup.LevelTwoService

// CreateLevelTwo 创建LevelTwo
// @Tags LevelTwo
// @Summary 创建LevelTwo
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body hierarchy.LevelTwo true "创建LevelTwo"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /levelTwo/createLevelTwo [post]
func (levelTwoApi *LevelTwoApi) CreateLevelTwo(c *gin.Context) {
	var levelTwo hierarchy.LevelTwo
	err := c.ShouldBindJSON(&levelTwo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	verify := utils.Rules{
		"Key":   {utils.NotEmpty()},
		"Value": {utils.NotEmpty()},
	}
	if err := utils.Verify(levelTwo, verify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := levelTwoService.CreateLevelTwo(&levelTwo); err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

// DeleteLevelTwo 删除LevelTwo
// @Tags LevelTwo
// @Summary 删除LevelTwo
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body hierarchy.LevelTwo true "删除LevelTwo"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /levelTwo/deleteLevelTwo [delete]
func (levelTwoApi *LevelTwoApi) DeleteLevelTwo(c *gin.Context) {
	var levelTwo hierarchy.LevelTwo
	err := c.ShouldBindJSON(&levelTwo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := levelTwoService.DeleteLevelTwo(levelTwo); err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage(err.Error(), c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

// DeleteLevelTwoByIds 批量删除LevelTwo
// @Tags LevelTwo
// @Summary 批量删除LevelTwo
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.IdsReq true "批量删除LevelTwo"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"批量删除成功"}"
// @Router /levelTwo/deleteLevelTwoByIds [delete]
func (levelTwoApi *LevelTwoApi) DeleteLevelTwoByIds(c *gin.Context) {
	var IDS request.IdsReq
	err := c.ShouldBindJSON(&IDS)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := levelTwoService.DeleteLevelTwoByIds(IDS); err != nil {
		global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}

// UpdateLevelTwo 更新LevelTwo
// @Tags LevelTwo
// @Summary 更新LevelTwo
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body hierarchy.LevelTwo true "更新LevelTwo"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /levelTwo/updateLevelTwo [put]
func (levelTwoApi *LevelTwoApi) UpdateLevelTwo(c *gin.Context) {
	var levelTwo hierarchy.LevelTwo
	err := c.ShouldBindJSON(&levelTwo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	verify := utils.Rules{
		"Key":   {utils.NotEmpty()},
		"Value": {utils.NotEmpty()},
	}
	if err := utils.Verify(levelTwo, verify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := levelTwoService.UpdateLevelTwo(levelTwo); err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// FindLevelTwo 用id查询LevelTwo
// @Tags LevelTwo
// @Summary 用id查询LevelTwo
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query hierarchy.LevelTwo true "用id查询LevelTwo"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /levelTwo/findLevelTwo [get]
func (levelTwoApi *LevelTwoApi) FindLevelTwo(c *gin.Context) {
	var levelTwo hierarchy.LevelTwo
	err := c.ShouldBindQuery(&levelTwo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if relevelTwo, err := levelTwoService.GetLevelTwo(levelTwo.ID); err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"relevelTwo": relevelTwo}, c)
	}
}

// GetLevelTwoList 分页获取LevelTwo列表
// @Tags LevelTwo
// @Summary 分页获取LevelTwo列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query hierarchyReq.LevelTwoSearch true "分页获取LevelTwo列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /levelTwo/getArticle [get]
func (levelTwoApi *LevelTwoApi) GetArticle(c *gin.Context) {
	var pageInfo hierarchyReq.LevelTwoArticleSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	userID := utils.GetUserID(c)
	if list, err := levelTwoService.GetArticle(pageInfo, userID); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(
			gin.H{
				"list": list,
			}, "获取成功", c)
	}
}

// GetLevelTwoList 分页获取LevelTwo列表
// @Tags LevelTwo
// @Summary 分页获取LevelTwo列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query hierarchyReq.LevelTwoSearch true "分页获取LevelTwo列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /levelTwo/getLevelTwoList [get]
func (levelTwoApi *LevelTwoApi) GetLevelTwoList(c *gin.Context) {
	var pageInfo hierarchyReq.LevelTwoSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := levelTwoService.GetLevelTwoInfoList(pageInfo); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "获取成功", c)
	}
}

// GetLevelTwoListByIds 根据ID集合获取二级目录列表
// @Tags LevelTwo
// @Summary 根据ID集合获取二级目录列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query hierarchyReq.LevelTwoSearch true "根据ID集合获取二级目录列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /levelTwo/getLevelTwoListByIds [get]
func (levelTwoApi *LevelTwoApi) GetLevelTwoListByIds(c *gin.Context) {
	var pageInfo hierarchyReq.LevelTwoIdsSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := levelTwoService.GetLevelTwoListByIds(pageInfo); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "获取成功", c)
	}
}
